/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import model.entities.Report;
import model.entities.ReportParameters;
import model.sessionBeans.ReportSessionBean;
import org.primefaces.context.RequestContext;
import org.primefaces.extensions.model.dynaform.DynaFormControl;
import org.primefaces.extensions.model.dynaform.DynaFormLabel;
import org.primefaces.extensions.model.dynaform.DynaFormModel;
import org.primefaces.extensions.model.dynaform.DynaFormRow;

/**
 *
 * @author maged.abdelmoeti
 */
@ManagedBean(name="dynaFormController")
@ViewScoped
public class DynaReportManagedBean   implements Serializable
{
    /**
     * Creates a new instance of NewJSFManagedBean
     */
 private static final long serialVersionUID = 1L;

  private String name="Hello world !" ;
    
  
   @EJB
   ReportSessionBean reportBean;

    
   @PostConstruct
   public void init()
   {

              List<Report> reports = reportBean.findAll();
       for(int i=0;i<reports.size();i++)
       {
                  Report report= reports.get(i);
                  List<ReportParameters> reportParameters = report.getReportParametersList();
                  System.out.println(report.getName());
                  for(int l=0;l<reportParameters.size();l++)
                  {
                      System.out.println(reportParameters.get(l).getId()+" "+reportParameters.get(l).getParamName()
                              +" "+reportParameters.get(l).getParamType()+" "+reportParameters.get(l).getParamValue()
                              );
                  }
       }
       
       Report report = (Report)reports.get(0);
       List reportParametersList = report.getReportParametersList();
       
       
       
       model = new DynaFormModel(); 
       int e=0 ;
       DynaFormRow row = model.createRegularRow();
       
        for(int l=0;l<reportParametersList.size();l++)
        {
                ReportParameters reportParameter = (ReportParameters)reportParametersList.get(l) ;
                
                if(e==3)
                {
                   row = model.createRegularRow();  
                   e = 0 ;
                }

                DynaFormLabel label11 = row.addLabel(reportParameter.getParamName(), 1, 1);  
                DynaFormControl control12 = row.addControl(reportParameter,reportParameter.getParamType(), 1, 1);  
                label11.setForControl(control12);  
                e++ ;
        }
   }
   
 
      
    private DynaFormModel model;  
  
    private static List<SelectItem> LANGUAGES = new ArrayList<SelectItem>();  
  
    public DynaReportManagedBean() 
    {              
    }
  
    public DynaFormModel getModel() {  
        return model;  
    }  
  

    public void submitFor() {  
        /*FacesMessage.Severity sev = FacesContext.getCurrentInstance().getMaximumSeverity();  
        boolean hasErrors = (sev != null && (FacesMessage.SEVERITY_ERROR.compareTo(sev) >= 0));  
  
        RequestContext requestContext = RequestContext.getCurrentInstance();  
        requestContext.addCallbackParam("isValid", !hasErrors);  
  
        return null;  */
        List<DynaFormControl> controls = model.getControls();
        System.out.println(controls.get(0).getKey());
    }  
  
    public List<SelectItem> getLanguages() {  
        if (LANGUAGES.isEmpty()) {  
            LANGUAGES.add(new SelectItem("en", "English"));  
            LANGUAGES.add(new SelectItem("de", "German"));  
            LANGUAGES.add(new SelectItem("ru", "Russian"));  
            LANGUAGES.add(new SelectItem("tr", "Turkish"));  
        }  
  
        return LANGUAGES;  
    }  
   
     public List<ReportParameters> getReportParameters() {  
        if (model == null) {  
            return null;  
        }  
  
        List<ReportParameters> reportParametersList = new ArrayList<ReportParameters>();  
        for (DynaFormControl dynaFormControl : model.getControls()) {  
            reportParametersList.add((ReportParameters) dynaFormControl.getData());  
        }  
  
        return reportParametersList;  
    } 
     
     
     
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
     
    
}
